﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain.Services
{
    public class RawDtTableService
    {
        /// <summary>
        /// 获取所有数据表
        /// </summary>
        /// <returns></returns>
        public ListResult<Entities.EDtTable> GetTables()
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.EDtTable> dtRep = new EntityRepository<Entities.EDtTable>(dbContext);

                List<Entities.EDtTable> list = dtRep.Query().OrderBy(p => p.Name).ToList();
                return new ListResult<Entities.EDtTable>(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError), list);
            }
        }
        
        public ServiceResult<DataTable> GetData(Guid appId, string token, Guid tableId)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.EAppAuthorization> aaRep = new EntityRepository<Entities.EAppAuthorization>(dbContext);
                Entities.EAppAuthorization eAppAuthorization = aaRep.Get(p => p.AppId == appId && p.Token == token);
                if(eAppAuthorization == null)
                {
                    return new ServiceResult<DataTable>(false, ErrorCode.AppNotAuthorizated, ErrorCode.String(ErrorCode.AppNotAuthorizated));
                }

                EntityRepository<Entities.EDtTable> dtRep = new EntityRepository<Entities.EDtTable>(dbContext);
                Entities.EDtTable eTable = dtRep.Get(p => p.Id == tableId);
                if(eTable == null)
                {
                    return new ServiceResult<DataTable>(false, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
                }

                EntityRepository<Entities.EDtTableColumn> dtcRep = new EntityRepository<Entities.EDtTableColumn>(dbContext);
                List<Entities.EDtTableColumn> vDtColumns = dtcRep.GetList(p => p.DtTableId == eTable.Id);

                List<string> columns = new List<string>();
                for (int index = 0; index < vDtColumns.Count; index++)
                {
                    Entities.EDtTableColumn vDtColumn = vDtColumns[index];
                    columns.Add(vDtColumn.Name);
                }

                int tTotal = 0;
                System.Data.DataTable dataTable = CPPEI.ETL.Domain.Util.DtTableUtil.GetDataFromTable(dbContext,
                    appId,
                    eTable.Id,
                    eTable.ActualName,
                    columns);

                return new ServiceResult<DataTable>(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError), dataTable);
            }
        }

        public ServiceResult CreateExtractHistory(Guid appId, string token, Guid tableId, Guid batchId, List<Guid> dataIdList)
        {
            if (dataIdList == null || dataIdList.Count == 0)
            {
                return new ServiceResult(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError));
            }

            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.EDtTable> dtRep = new EntityRepository<Entities.EDtTable>(dbContext);
                Entities.EDtTable eDtTable = dtRep.Get(p => p.Id == tableId);
                if(eDtTable == null)
                {
                    return new ServiceResult(true, ErrorCode.RawDataTableNotExist, ErrorCode.String(ErrorCode.RawDataTableNotExist));
                }

                EntityRepository<Entities.EAppAuthorization> aaRep = new EntityRepository<Entities.EAppAuthorization>(dbContext);
                if(aaRep.Count(p=>p.Id == appId && p.Token == token) == 0)
                {
                    return new ServiceResult(true, ErrorCode.AppNotAuthorizated, ErrorCode.String(ErrorCode.AppNotAuthorizated));
                }

                EntityRepository<Entities.ERawDataExtractHistory> rdehRep = new EntityRepository<Entities.ERawDataExtractHistory>(dbContext);

                for (int index = 0; index < dataIdList.Count; index++)
                {
                    Entities.ERawDataExtractHistory eHistory = new Entities.ERawDataExtractHistory();
                    eHistory.Id = Guid.NewGuid();
                    eHistory.DataId = dataIdList[index];
                    eHistory.AppId = appId;
                    eHistory.DtTableId = eDtTable.Id;
                    eHistory.Creator = "system";
                    eHistory.CreatedTime = DateTime.Now;

                    rdehRep.Create(eHistory);
                }
                return new ServiceResult(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError));
            }
        }

        public ListResult<Entities.EDtTableColumn> GetSchema(Guid appId, string token, Guid tableId)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.EDtTable> dtRep = new EntityRepository<Entities.EDtTable>(dbContext);
                Entities.EDtTable eDtTable = dtRep.Get(p => p.Id == tableId);
                if(eDtTable == null)
                {
                    return new ListResult<Entities.EDtTableColumn>(true, ErrorCode.RawDataTableNotExist, ErrorCode.String(ErrorCode.RawDataTableNotExist));
                }

                EntityRepository<Entities.EAppAuthorization> aaRep = new EntityRepository<Entities.EAppAuthorization>(dbContext);
                if(aaRep.Count(p=>p.AppId == appId && p.Token == token) == 0)
                {
                    return new ListResult<Entities.EDtTableColumn>(true, ErrorCode.AppNotAuthorizated, ErrorCode.String(ErrorCode.AppNotAuthorizated));
                }

                EntityRepository<Entities.EDtTableColumn> dtcRep = new EntityRepository<Entities.EDtTableColumn>(dbContext);

                List<Entities.EDtTableColumn> list = dtcRep.GetList(p => p.DtTableId == eDtTable.Id);

                return new ListResult<Entities.EDtTableColumn>(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError), list);
            }
        }
    }
}
